package com.example.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.management.common.QueryPageParam;
import com.example.management.common.Result;
import com.example.management.entity.Employee;
import com.example.management.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //查询
    @GetMapping("/list")
    public List<Employee> list() { return employeeService.list(); }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Employee employee) {
        return employeeService.save(employee) ? Result.success() : Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Employee employee) {return employeeService.updateById(employee) ? Result.success() : Result.fail();}
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Employee employee) {
        return employeeService.updateById(employee);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOfMod(@RequestBody Employee employee) {
        return employeeService.saveOrUpdate(employee);
    }
    //删除
    @GetMapping("/delete")
    public Result delete(String eno) {
        return employeeService.removeById(eno) ? Result.success() : Result.fail();
    }
    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String eno) {
        List<Employee> list = employeeService.lambdaQuery().eq(Employee::getEno, eno).list();
        return list.isEmpty() ? Result.fail() : Result.success();
    }
    @GetMapping("/findByPhone")
    public Result findByPhone(@RequestParam String ephone) {
        List<Employee> list = employeeService.lambdaQuery().eq(Employee::getEphone, ephone).list();
        return list.isEmpty() ? Result.fail() : Result.success();
    }
    //查询（模糊、匹配）
    @PostMapping("/listP")
    public Result listP(@RequestBody Employee employee) {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Employee::getEno, employee.getEno());
        return Result.success(employeeService.list(lambdaQueryWrapper));
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);
        System.out.println("num==" + query.getPageNum());
        System.out.println("size==" + query.getPageSize());
        HashMap param = query.getParam();
        String no = (String) param.get("eno");
        String name = (String) param.get("ename");

        Page<Employee> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(no) && !"null".equals(no)) {
            lambdaQueryWrapper.eq(Employee::getEno, no);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Employee::getEname, name);
        }

        IPage<Employee> data = employeeService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + data.getTotal());

        return Result.success(data.getRecords(), data.getTotal());
    }
}